home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / c / rel_hp300.c < prev    next >
C/C++ Source or Header  |  1990-01-07  |  5KB  |  202 lines

  1.  
  2. #undef NSYMS
  3. #define NSYMS(hdr) count_symbols(&hdr,fp)
  4. #ifndef dprintf
  5. #define dprintf(a,b) 
  6. #endif
  7.  
  8. /* the routines other than relocate are also used in rsym.c */
  9.  
  10.  
  11. #ifdef IN_RSYM
  12. #define temp_malloc  malloc
  13. #else
  14.  
  15. relocate()
  16. {
  17.   char *where;
  18.   {
  19.         unsigned int new_value;
  20.         where = the_start + relocation_info.r_address;
  21.            switch(relocation_info.r_segment){
  22.              case RNOOP: goto DONT;
  23.              case REXT: 
  24.                   new_value = symbol_table[relocation_info.
  25.                              r_symbolnum].n_value;
  26.                 break;
  27.              case RDATA: 
  28.                 case RBSS: 
  29.              case RTEXT: new_value= (int)start_address; break;
  30.              default:
  31.             dprintf(relocation_info.r_address = %d, 
  32.                 relocation_info.r_address);
  33.             printf(
  34. "\nRel_Info {r_segment = %x, r_symbolnum= %x, r_address = %d} -- Ignored", 
  35.                       relocation_info.r_segment, 
  36.                       relocation_info.r_symbolnum,
  37.                       relocation_info.r_address); 
  38.             fflush(stdout);
  39.             goto DONT;
  40.         };
  41.         switch(relocation_info.r_length){
  42.            case 0: *( char *)where = new_value + *( char *) where;
  43.                break;
  44.            case 1: *( short *)where = new_value + *( short *) where;
  45.                break;
  46.            case 2: *( long *)where = new_value + *( long *) where;
  47.                break;
  48.         }
  49. DONT:           ;
  50.           }
  51. }
  52.  
  53.  
  54.  
  55. #ifdef PRIVATE_FASLINK
  56.  
  57. int
  58. faslink(faslfile, ldargstring)
  59. object faslfile, ldargstring;
  60. {
  61.     struct exec tmpheader, faslheader;
  62.  
  63.     FILE *fp; TABL *table;
  64.     char filename[MAXPATHLEN];
  65.     char ldargstr[MAXPATHLEN];
  66.     char stbfilename[32];
  67.     char tmpfilename[32];
  68.     char command[MAXPATHLEN * 2];
  69.     char buf[BUFSIZ], *p;
  70.     int i, res;
  71.     object tmpfile, data;
  72.     object *old_vs_base = vs_base;
  73.     object *old_vs_top = vs_top;
  74.  
  75.     coerce_to_filename(ldargstring, ldargstr);
  76.     coerce_to_filename(faslfile, filename);
  77.  
  78. /* Print out symbol table */
  79.     sprintf(stbfilename, "/tmp/stb%d", getpid());
  80.     fp = fopen(stbfilename, "w");
  81.     for(i = 0, p = (char *)&tmpheader; i < sizeof(struct exec); i++) 
  82.        *p++ = '\0';
  83.     tmpheader.a_magic.system_id = HP9000S200_ID;
  84.     tmpheader.a_magic.file_type = RELOC_MAGIC;
  85.     tmpheader.a_stamp = 2;
  86.  
  87.     fwrite(&tmpheader, sizeof(struct exec), 1, fp);
  88.     table = c_table.ptable;
  89.     for(i = 0; i < c_table.length; i++) {
  90.        struct nlist_ nbuf; int len;
  91.        char *string; unsigned int address;
  92.        string  =(*table)[i].string;
  93.        address =(*table)[i].address;
  94.        len = strlen(string);
  95.        if (((strncmp(string,"_end",  4) == NULL) && (len == 4)) ||
  96.            ((strncmp(string,"_etext",6) == NULL) && (len == 6)) ||
  97.            ((strncmp(string,"_edata",6) == NULL) && (len == 6)))
  98.           continue;
  99.        nbuf.n_value = address; 
  100.        nbuf.n_type = N_ABS | N_EXT;
  101.        nbuf.n_length = len;
  102.        nbuf.n_almod  = 0;
  103.        nbuf.n_unused = 0;
  104.        fwrite(&nbuf,sizeof(nbuf),1,fp);
  105.        fwrite(string,len,1,fp);
  106.        tmpheader.a_lesyms += sizeof(struct nlist_) + len;
  107.     }
  108.     fseek(fp,0,0);
  109.     fwrite(&tmpheader, sizeof(struct exec), 1, fp);
  110.     fclose(fp);
  111.  
  112.     sprintf(tmpfilename, "/tmp/fasl%d", getpid());
  113.     sprintf(command,
  114.         "ld -r -o %s -x %s %s %s -h _edata -h _etext", 
  115.          tmpfilename,
  116.          stbfilename,
  117.          filename,
  118.          ldargstr);
  119.  
  120.     if (system(command) != 0)
  121.         FEerror("The linkage editor failed.", 0);
  122.  
  123.     unlink(stbfilename);
  124.  
  125.     faslfile = open_stream(faslfile, smm_input, Cnil, Kerror);
  126.     vs_push(faslfile);
  127.     fp = faslfile->sm.sm_fp;
  128.     fread(&faslheader, sizeof(faslheader), 1, fp);
  129.     fseek(fp,RDATA_OFFSET(faslheader)+faslheader.a_drsize,0);
  130.  
  131.     { FILE *tmpfp;
  132.       int fpthis,fpend;
  133.       fpthis = ftell(fp);
  134.       fseek(fp,0,2);
  135.       fpend  = ftell(fp);
  136.       fseek(fp,fpthis,0);
  137.       tmpfp = fopen(tmpfilename,"a+");
  138.       fseek(tmpfp,0,2);
  139.       filecpy(tmpfp,fp,fpend-fpthis);
  140.       fclose(tmpfp);
  141.     }
  142.  
  143.     close_stream(faslfile, TRUE);
  144.  
  145.     tmpfile = make_simple_string(tmpfilename);
  146.     vs_push(tmpfile);
  147.  
  148.     res = fasload(tmpfile);
  149.  
  150.     unlink(tmpfilename);
  151.     vs_base = old_vs_base;
  152.     vs_top = old_vs_top;
  153.  
  154.     return(res);
  155. }
  156. #endif
  157. #endif
  158.  
  159.  
  160. count_symbols(phdr,fp)
  161. struct exec *phdr;
  162. FILE *fp;
  163. {int nsyms,i;
  164.  fseek(fp,(int)(LESYM_OFFSET(*phdr)), 0);
  165.  for(i = phdr->a_lesyms, nsyms = 0; i > 0; nsyms++) {
  166.    struct syment tmp;
  167.    fread((char *)&tmp, SYMESZ, 1, fp); i -= SYMESZ;
  168.    fseek(fp,(int)tmp.n_length,1); i -= tmp.n_length;
  169.  }
  170.  return (nsyms);
  171. }
  172.  
  173.  
  174. #define READ_IN_STRING_TABLE(fp,size) \
  175.   read_in_string_table(fp,&fileheader,size)
  176.  
  177. char *
  178. read_in_string_table(fp,pfileheader,string_size)
  179. FILE *fp;
  180. struct exec *pfileheader;
  181. int string_size ;
  182.  
  183. {
  184.   char *p,*ans; 
  185.   int slen,i,j;
  186.   ans=p = temp_malloc((unsigned int)string_size);
  187.   dprintf( string table leng = %d, string_size);
  188.   fseek(fp,LESYM_OFFSET(*pfileheader),  0);
  189.   for (i = pfileheader->a_lesyms,j=0;  i > 0;  i=i- slen-SYMESZ)
  190.     {
  191.       fseek(fp,SYMESZ, 1);
  192.       slen = symbol_table[j++].n_length;
  193.       fread(p,slen,1,fp);
  194.       *((p)+slen) = '\0';
  195.       dprintf( p  %s , p);
  196.       dprintf( slen  %d,slen);
  197.       
  198.       p += slen + 1;
  199.     }
  200.   return (ans);
  201. }
  202.